package java_1_24.leetcode;

import java.util.Stack;

/**
 * 最小栈
 */
public class Num155_MinStack {
    private Stack<Integer> s1=new Stack<>();
    private Stack<Integer> s2=new Stack<>();
    public Num155_MinStack() {
    }

    public void push(int val) {
        s1.push(val);
        if(s2.isEmpty()){
            s2.push(val);
        }else{
            //比较当前val和s2栈顶元素，将较小的入栈s2
            int tmp=s2.peek();
            s2.push(Math.min(tmp,val));//取最小值入栈s2
        }
    }

    public void pop() {
        s1.pop();//题目给出是非空栈，不用判空，直接出栈，s1s2都出
        s2.pop();
    }

    public int top() {
        return s1.peek();//返回栈顶元素但不出栈
    }

    public int getMin() {
        return s2.peek();//返回s2栈顶元素【最小值在s2存着】
    }
}
